<?php

/**
 * project actions.
 *
 * @package    student_manager
 * @subpackage project
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2288 2006-10-02 15:22:13Z fabien $
 */
class projectActions extends autoprojectActions
{

  public function executeEdit()
  {
    $this->getResponse()->setTitle('WMS-Edit-Project');
    $this->project = $this->getProjectOrCreate();

    if ($this->getRequest()->isMethod('post'))
    {
      $this->updateProjectFromRequest();

      $c = new Criteria();
      $c->add(ProjectPeer::SALENO, $this->project->getSaleno());
      $prjList = ProjectPeer::doSelect($c);

      if (count($prjList)>1 || (count($prjList) ==1 && $this->project->getId() != $prjList[0]->getId()))
      {
          $this->getRequest()->setError('edit', 'Could not save the edited Projects Because sale number is duplicated.');
          $this->labels = $this->getLabels();
          return ;
      }

      try
      {
        $this->saveProject($this->project);
      }
      catch (PropelException $e)
      {
        $this->getRequest()->setError('edit', 'Could not save the edited Projects.');
        return $this->forward('project', 'list');
      }

      $this->getUser()->setFlash('notice', 'Your modifications have been saved');

      if ($this->getRequestParameter('save_and_add'))
      {
        return $this->redirect('project/create');
      }
      else if ($this->getRequestParameter('save_and_list'))
      {
        return $this->redirect('project/list');
      }
      else
      {
        return $this->redirect('project/edit?id='.$this->project->getId());
      }
    }
    else
    {
      $this->labels = $this->getLabels();
    }
  }

  public function executeList()
  {
     $this->getResponse()->setTitle('WMS-Project-List'); 
    $this->processSort();

    $this->processFilters();

    $this->filters = $this->getUser()->getAttributeHolder()->getAll('sf_admin/project/filters');

    // pager
    $this->pager = new sfPropelPager('Project', 25);
    $c = new Criteria();
    $c->addDescendingOrderByColumn( ProjectPeer::SALENO);
    
    $this->addSortCriteria($c);
    
    $this->addFiltersCriteria($c);
    $this->pager->setCriteria($c);
    $this->pager->setPage($this->getRequestParameter('page', $this->getUser()->getAttribute('page', 1, 'sf_admin/project')));
    $this->pager->init();
    // save page
    if ($this->getRequestParameter('page')) {
        $this->getUser()->setAttribute('page', $this->getRequestParameter('page'), 'sf_admin/project');
    }
  }


  
  protected function addFiltersCriteria($c)
  {
    if (isset($this->filters['name_is_empty']))
    {
      $criterion = $c->getNewCriterion(ProjectPeer::NAME, '');
      $criterion->addOr($c->getNewCriterion(ProjectPeer::NAME, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['name']) && $this->filters['name'] !== '')
    {
      $c->add(ProjectPeer::NAME, '%'.strtr($this->filters['name'], '*', '%').'%', Criteria::LIKE);
    }
    if (isset($this->filters['client_id_is_empty']))
    {
      $criterion = $c->getNewCriterion(ProjectPeer::CLIENT_ID, '');
      $criterion->addOr($c->getNewCriterion(ProjectPeer::CLIENT_ID, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['client_id']) && $this->filters['client_id'] !== '')
    {
      $c->add(ProjectPeer::CLIENT_ID, $this->filters['client_id']);
    }
    if (isset($this->filters['saleno_is_empty']))
    {
      $criterion = $c->getNewCriterion(ProjectPeer::SALENO, '');
      $criterion->addOr($c->getNewCriterion(ProjectPeer::SALENO, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['saleno']) && $this->filters['saleno'] !== '')
    {
      $c->add(ProjectPeer::SALENO, '%'.strtr($this->filters['saleno'], '*', '%').'%', Criteria::LIKE);
    }
    if (isset($this->filters['created_at_is_empty']))
    {
      $criterion = $c->getNewCriterion(ProjectPeer::CREATED_AT, '');
      $criterion->addOr($c->getNewCriterion(ProjectPeer::CREATED_AT, null, Criteria::ISNULL));
      $c->add($criterion);
    }
    else if (isset($this->filters['created_at']))
    {
      if (isset($this->filters['created_at']['from']) && $this->filters['created_at']['from'] !== '')
      {
        $criterion = $c->getNewCriterion(ProjectPeer::CREATED_AT, $this->filters['created_at']['from'], Criteria::GREATER_EQUAL);
      }
      if (isset($this->filters['created_at']['to']) && $this->filters['created_at']['to'] !== '')
      {
        if (isset($criterion))
        {
          $criterion->addAnd($c->getNewCriterion(ProjectPeer::CREATED_AT, $this->filters['created_at']['to'], Criteria::LESS_EQUAL));
        }
        else
        {
          $criterion = $c->getNewCriterion(ProjectPeer::CREATED_AT, $this->filters['created_at']['to'], Criteria::LESS_EQUAL);
        }
      }

      if (isset($criterion))
      {
        $c->add($criterion);
      }
    }
  }

}
